//
//  DataManager.m
//  UMDReader
//
//  Created by 张 霄鹤 on 10-1-10.
//  Copyright 2010 xhzhang. All rights reserved.
//

#import "DataManager.h"

#import "UMDFileReader.h"

static DataManager *dataManagerInstance = nil;

@implementation DataManager

+ (DataManager *)sharedDataManager{
	if(dataManagerInstance == nil){
		dataManagerInstance = [[DataManager alloc] init];
	}
	return dataManagerInstance;
}

+ (id)alloc{
	return [super alloc];
}

- (id)init{
	if(self = [super init]){
		resPath = [[NSBundle mainBundle] resourcePath];
	}
	return self;
}

- (UMDBook *)loadBook:(NSString *)filePath{
	UMDBook *ret = nil;
	if(filePath){
		NSString *fullPath;
		if([filePath hasPrefix:@"res://"]){
			// TODO 加载bundle中的资源
			fullPath = [NSString stringWithFormat:@"%@/%@",resPath,[filePath substringFromIndex:6]];
			// NSLog(@"res = %@",fullPath);
			// [filePath substringToIndex:6];
		} else {
			// TODO 加载Document中的资源
			NSString *doc = [self applicationDocumentsDirectory];
			fullPath = [NSString stringWithFormat:@"%@/%@",doc,filePath];
			// NSLog(@"doc = %@",fullPath);
		}
		if([fullPath hasSuffix:@".umd"]){
			UMDFileReader *reader = [[UMDFileReader alloc] init];
			ret = [reader read:fullPath];
			[reader release];
		}
	}
	return ret;
}

- (EBFile *)loadEBook:(NSString *)filePath{
	EBFile *ret = nil;
	if(filePath){
		NSString *fullPath;
		if([filePath hasPrefix:@"res://"]){
			// TODO 加载bundle中的资源
			fullPath = [NSString stringWithFormat:@"%@/%@",resPath,[filePath substringFromIndex:6]];
			// NSLog(@"res = %@",fullPath);
			// [filePath substringToIndex:6];
		} else {
			// TODO 加载Document中的资源
			NSString *doc = [self applicationDocumentsDirectory];
			fullPath = [NSString stringWithFormat:@"%@/%@",doc,filePath];
			// NSLog(@"doc = %@",fullPath);
		}
		if([fullPath hasSuffix:@".ebf"]){
			EBFile *eb = [[EBFile alloc] init];
			NSData *data = [[NSData alloc] initWithContentsOfFile:fullPath];
			if(data){
				BEDataInputStream *din = [[BEDataInputStream alloc] initWithData:data];
				[eb read:din];
				[din release];
				ret = [eb autorelease];
				[data release];
			}
		}
	}
	return ret;
}

#pragma mark -
#pragma mark Application's Documents directory

/**
 Returns the path to the application's Documents directory.
 */
- (NSString *)applicationDocumentsDirectory {
	return [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
}

#pragma mark -
#pragma mark lookupfile
- (NSArray *)lookupFiles:(NSString *)ext{
	NSString *docpath = [self applicationDocumentsDirectory];
	NSString *umdpath = [docpath stringByAppendingString:@"/umds"];
	NSLog(umdpath);
	NSFileManager *fm = [NSFileManager defaultManager];
	BOOL dir = YES;
	// 此处的dir注意一定要 &dir方式
	BOOL ex = [fm fileExistsAtPath:umdpath isDirectory:&dir];
	if(!ex){
		NSError *err = nil;
		BOOL ok = [fm createDirectoryAtPath:umdpath withIntermediateDirectories:NO attributes:nil error:&err];
		if(!ok){
			return nil;
		}
	}
	NSDirectoryEnumerator *dirEnum = [fm enumeratorAtPath:umdpath];
	NSMutableArray *files = nil;
	if(dirEnum){
		NSString *file;
		files = [NSMutableArray array];
		while (file = [dirEnum nextObject]) {
			if ([[file pathExtension] isEqualToString:ext]) {
				[files addObject:file];
			}
		}
	}
	return files;	
}

#pragma mark -
- (void)dealloc{
	[super dealloc];
}

@end
